﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using sexhealth.console.service.post;
using sexhealth.console.service.activity.vo;
using com.pro2e.web.util.page;
using System.Text;
using com.pro2e.web;

public partial class Activity_In : System.Web.UI.Page
{
    ActivityService activityService = new ActivityService();
    HttpHelper httpHelper = new HttpHelper();

    protected void Page_Load(object sender, EventArgs e)
    {
    }

    protected void btnSend_Click(object sender, ImageClickEventArgs e)
    {
        string checkMsg = GetCheckMsg();
        if (!string.IsNullOrEmpty(checkMsg))
        {
            ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "js", JavascriptUtil.AlertJS(checkMsg), false);
        }
        else
        {
            AnswerVO answerVO = new AnswerVO();
            answerVO.ActivityId = AnswerVO.LatestActivityId;
            answerVO.AnswerType = AnswerType.SignUp;
            answerVO.Ans1_1 = txtAns1_1.Text.Trim();
            answerVO.Ans1_2 = txtAns1_2.Text.Trim();
            answerVO.Ans2_1 = txtAns2_1.Text.Trim();
            answerVO.Ans3_1 = txtAns3_1.Text.Trim();
            answerVO.Ans4_1 = txtAns4_1.Text.Trim();
            answerVO.Name = txtName.Text.Trim();
            if (rdoSexM.Checked)
            {
                answerVO.Sex = "男";
            }
            else if (rdoSexF.Checked)
            {
                answerVO.Sex = "女";
            }
            answerVO.Age = txtAge.Text.Trim();
            answerVO.Phone = txtPhone.Text.Trim();
            answerVO.Email = txtEmail.Text.Trim();
            answerVO.Address = txtAddress.Text.Trim();
            answerVO.Option1_1 = ckbOption1_1.Checked;
            answerVO.Option1_2 = ckbOption1_2.Checked;
            answerVO.Option1_3 = ckbOption1_3.Checked;
            answerVO.CreatedIP = httpHelper.GetUserIp(Context);

            activityService.CreateAnswer(answerVO);

            ClearUI();

            ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "js", JavascriptUtil.AlertJS("報名完成，謝謝您的參與!"), false);
        }
    }

    private string GetCheckMsg()
    {
        StringBuilder msg = new StringBuilder();

        if (string.IsNullOrEmpty(txtAns1_1.Text.Trim()) || string.IsNullOrEmpty(txtAns1_2.Text.Trim()))
        {
            msg.Append("請填寫徵文內容1\\n");
        }
        else
        {
            if (txtAns1_1.Text.Trim().Length > 100 || txtAns1_2.Text.Trim().Length > 100)
            {
                msg.Append("徵文內容1，字數限制100字內\\n");
            }
        }

        if (string.IsNullOrEmpty(txtAns2_1.Text.Trim()))
        {
            msg.Append("請填寫徵文內容2\\n");
        }
        else
        {
            if (txtAns2_1.Text.Trim().Length > 100)
            {
                msg.Append("徵文內容2，字數限制100字內\\n");
            }
        }

        if (string.IsNullOrEmpty(txtAns3_1.Text.Trim()))
        {
            msg.Append("請填寫徵文內容3\\n");
        }
        else
        {
            if (txtAns3_1.Text.Trim().Length > 100)
            {
                msg.Append("徵文內容3，字數限制100字內\\n");
            }
        }

        if (string.IsNullOrEmpty(txtAns4_1.Text.Trim()))
        {
            msg.Append("請填寫徵文內容4\\n");
        }
        else
        {
            if (txtAns4_1.Text.Trim().Length > 100)
            {
                msg.Append("徵文內容4，字數限制100字內\\n");
            }
        }

        if (string.IsNullOrEmpty(txtName.Text.Trim()))
        {
            msg.Append("請填寫姓名\\n");
        }

        if (!rdoSexM.Checked && !rdoSexF.Checked)
        {
            msg.Append("請選擇性別\\n");
        }

        if (string.IsNullOrEmpty(txtAge.Text.Trim()))
        {
            msg.Append("請填寫年齡\\n");
        }
        else
        {
            try
            {
                int age = int.Parse(txtAge.Text.Trim());
                if (age < 40)
                {
                    msg.Append("年齡必須40歲以上\\n");
                }
            }
            catch (Exception ex)
            {
                msg.Append("年齡格式不正確，請填寫整數\\n");
            }
        }

        if (string.IsNullOrEmpty(txtPhone.Text.Trim()))
        {
            msg.Append("請填寫連絡電話/手機\\n");
        }

        if (string.IsNullOrEmpty(txtEmail.Text.Trim()))
        {
            msg.Append("請填寫電子信箱\\n");
        }

        if (string.IsNullOrEmpty(txtAddress.Text.Trim()))
        {
            msg.Append("請填寫聯絡地址\\n");
        }

        if (!ckbOption1_1.Checked && !ckbOption1_2.Checked && !ckbOption1_3.Checked)
        {
            msg.Append("請您至少選擇一項建議，謝謝。\\n");
        }

        return msg.ToString();
    }

    private void ClearUI()
    {
        txtAns1_1.Text = string.Empty;
        txtAns1_2.Text = string.Empty;
        txtAns2_1.Text = string.Empty;
        txtAns3_1.Text = string.Empty;
        txtAns4_1.Text = string.Empty;
        txtName.Text = string.Empty;
        rdoSexM.Checked = false;
        rdoSexF.Checked = false;
        txtAge.Text = string.Empty;
        txtPhone.Text = string.Empty;
        txtEmail.Text = string.Empty;
        txtAddress.Text = string.Empty;
        ckbOption1_1.Checked = false;
        ckbOption1_2.Checked = false;
        ckbOption1_3.Checked = false;
    }
}